package com.example.yimai.controller;

import com.example.yimai.entity.Address;
import com.example.yimai.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 添加地址
     * @param token token
     * @param params 地址参数
     *               - address 地址
     *               - receiveName 收货人
     *               - mobile 手机号
     *               - remark 备注
     * @return
     */
    @PostMapping("/add")
    public String addAddress(@RequestHeader("Authorization") String token,
                             @RequestParam Map<String, Object> params) {
        return addressService.addAddress(token, params);
    }

    @RequestMapping("/delete/{id}")
    public String deleteAddress(@PathVariable Integer id) {
        return addressService.deleteAddress(id);
    }

    /**
     * 修改地址
     * @param params 地址参数
     *               - id 地址id(必要参数)
     *               - address 地址
     *               - receiveName 收货人
     *               - mobile 手机号
     *               - remark 备注
     * @return
     */
    @RequestMapping("/update")
    public String updateAddress(@RequestParam Map<String, Object> params) {
        return addressService.updateAddress(params);
    }

    @RequestMapping("/get/{id}")
    public Address getAddress(@PathVariable Integer id) {
        return addressService.getAddress(id);
    }

    @RequestMapping("/getDefault")
    public Address getDefaultAddress(@RequestAttribute("id") String userId) {
        return addressService.getDefaultAddress(userId);
    }

    @RequestMapping("/getAll")
    public List<Address> getAllAddress(@RequestHeader("Authorization") String token) {
        return addressService.getAllAddress(token);
    }

    @RequestMapping("/setDefault/{id}")
    public String setDefault(@RequestAttribute("id") String userId , @PathVariable String id) {
        return addressService.setDefault(userId,id);
    }

}
